package weaver.interfaces.xfjt.action;

import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.xfjt.utils.XfjtUtil;
import weaver.jobs.xfjt.duss.utils.WorkflowCreateHandler;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-10-07
 * @Description: 单次任务发起流程触发单词任务反馈流程
 * @ModifyLog:
 **/

public class WeaSingleTaskAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String currentDate = DateUtil.getCurrentDate();
        RecordSet rs = new RecordSet();
        writeLog(">>>>WeaSingleTaskAction" + requestid);
        String workflowId = "73";
        String workflowName = "单次任务反馈";
        String isNextFlow = "1";
        try {
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            if (rs.next()) {
                Map<String, String> requestMainMap = new HashMap<>();
                //任务发起人
                requestMainMap.put("rwfqr", Util.null2String(rs.getString("rwfqr")));
                //发起公司
                requestMainMap.put("fqgs", Util.null2String(rs.getString("fqgs")));
                //发起部门
                requestMainMap.put("fqbm", Util.null2String(rs.getString("fqbm")));
                //发起日期
                String fqrq = Util.null2String(rs.getString("fqrq"));
                requestMainMap.put("fqrq", fqrq);
                //任务开始日期
                requestMainMap.put("rwksrq", Util.null2String(rs.getString("rwksrq")));
                //要求完成日期
                requestMainMap.put("yqwcrq", Util.null2String(rs.getString("yqwcrq")));
                //任务查看人
                requestMainMap.put("rwckr", Util.null2String(rs.getString("rwckr")));
                //任务内容
                requestMainMap.put("rwnr", Util.null2String(rs.getString("rwnr")));
                //任务附件
                requestMainMap.put("rwfj", Util.null2String(rs.getString("rwfj")));
                //任务编号
                requestMainMap.put("rwbh", Util.null2String(rs.getString("lcbh")));
                //任务名称
                requestMainMap.put("rwmc", Util.null2String(rs.getString("rwmc")));
                writeLog(">>>>WeaSingleTaskAction>>>requestMainMap=" + requestMainMap.toString());
                //任务执行人
                String rwzxr = Util.null2String(rs.getString("rwzxr"));
                writeLog(">>>>WeaSingleTaskAction>>>rwzxr=" + rwzxr);
                String dataid = XfjtUtil.getDataidByrequestid(requestid);
                //台账数据ID
                requestMainMap.put("jmid", dataid);
                writeLog(">>>>WeaSingleTaskAction>>>dataid=" + dataid);
                String[] strs = rwzxr.split(",");
                for (String userid: strs) {
                    requestMainMap.put("rwzxr",userid);
                    ResourceComInfo rci = new ResourceComInfo();
                    String requestName = workflowName + "-" + rci.getLastname(userid) + "-" + fqrq;
                    WorkflowCreateHandler handler = new WorkflowCreateHandler();
                    String newrequestid = handler.WorkflowCreateByRequestMap( userid, workflowId, requestName, isNextFlow, requestMainMap );
                    String sql = "insert into uf_dcrwtz_dt1(mainid,fkr,fklc,sfyfk) values(?,?,?,?)";
                    rs.executeUpdate(sql, dataid,userid,newrequestid,1);
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
